package com.ecoolex.ying.space.core.manager;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.PickGoodsProductDAO;
import com.ecoolex.ying.space.entity.*;
import com.ecoolex.ying.space.entity.PickGoodsProduct.Column;
import com.ecoolex.ying.space.enums.PickGoodsStatus;
import com.ecoolex.ying.space.pojo.EditPickGoodsProductDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class PickGoodsProductManager {
    @Autowired
    private PickGoodsProductDAO dao;

    @Autowired
    private GoodsProductManager goodsProductManager;

    @Autowired
    private PickGoodsManager pickGoodsManager;

    long countByProductId(Integer productId) {
        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria().andProductIdEqualTo(productId).andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);
        return dao.countByExample(example);
    }

    void refresh(GoodsProduct product, String updateUser) {
        PickGoodsProduct record = new PickGoodsProduct();
        record.setSpecIds(product.getSpecIds());
        record.setSpecNames(product.getSpecNames());
        record.setSpecSorting(product.getSpecSorting());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria().andProductIdEqualTo(product.getProductId()).andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);

        dao.updateByExampleSelective(record, example, Column.specIds, Column.specNames, Column.specSorting, Column.updateUser, Column.updateTime);
    }

    void deleteByPickGoodsId(Integer pickGoodsId, String updateUser) {
        PickGoodsProduct record = new PickGoodsProduct();
        record.setDeleted(PickGoodsProduct.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria()
                .andPickGoodsIdEqualTo(pickGoodsId)
                .andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);

        dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
    }

    private PickGoodsProduct getByPickGoodsIdAndProductId(Integer pickGoodsId, Integer productId) {
        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria().andPickGoodsIdEqualTo(pickGoodsId).andProductIdEqualTo(productId).andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);
        return dao.selectOneByExample(example);
    }

    @Transactional
    public PickGoodsProduct create(PickGoods goods, Integer productId, EditPickGoodsProductDTO add, String createUser) {
        GoodsProduct product = goodsProductManager.getByGoodsIdAndProductId(goods.getGoodsId(), productId);
        BizAssert.isTrue(Check.notNull(product), "产品不存在");
        PickGoodsProduct old = getByPickGoodsIdAndProductId(goods.getPickGoodsId(), productId);
        BizAssert.isTrue(Check.isNull(old), "产品已存在");

        PickGoodsProduct record = new PickGoodsProduct();
        record.setPickGoodsId(goods.getPickGoodsId());
        record.setGoodsId(goods.getGoodsId());
        record.setProductId(productId);
        record.setSpecIds(product.getSpecIds());
        record.setSpecNames(product.getSpecNames());
        record.setSpecSorting(product.getSpecSorting());
        record.setName(add.getName());
        record.setImage(add.getImage());
        record.setCoded(add.getCoded());
        record.setCounterPrice(add.getCounterPrice());
        record.setRetailPrice(add.getRetailPrice());
        record.setWeight(add.getWeight());
        record.setParameters(Check.isNull(add.getParameters()) ? null : JSON.toJSONString(add.getParameters()));
        record.setStockNumber(0);
        record.setUsedStockNumber(0);
        record.setSoldNumber(0);
        record.setStatus(-1);
        record.setDeleted(PickGoodsProduct.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));

        dao.insert(record);

        // 刷新价格数据
        pickGoodsManager.refreshProductPrice(record.getPickGoodsId());

        // 验证上线数据
        pickGoodsManager.verifyProductOnline(record.getPickGoodsId());

        return record;
    }

    @Transactional
    public void update(Integer pickProductId, EditPickGoodsProductDTO edit, String updateUser) {
        PickGoodsProduct record = new PickGoodsProduct();
        record.setPickProductId(pickProductId);
        record.setName(edit.getName());
        record.setImage(edit.getImage());
        record.setCoded(edit.getCoded());
        record.setCounterPrice(edit.getCounterPrice());
        record.setRetailPrice(edit.getRetailPrice());
        record.setWeight(edit.getWeight());
        record.setParameters(Check.isNull(edit.getParameters()) ? null : JSON.toJSONString(edit.getParameters()));
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria().andPickProductIdEqualTo(pickProductId).andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.name,
                Column.image,
                Column.coded,
                Column.counterPrice,
                Column.retailPrice,
                Column.weight,
                Column.parameters,
                Column.updateUser,
                Column.updateTime);
        BizAssert.isTrue(row == 1, "修改优选产品失败");

        // 刷新价格数据
        pickGoodsManager.refreshProductPrice(record.getPickGoodsId());

        // 验证上线数据
        pickGoodsManager.verifyProductOnline(getById(pickProductId).getPickGoodsId());
    }

    @Autowired
    private DesignerProjectItemsProductManager designerProjectItemsProductManager;
    @Autowired
    private DesignerProjectItemsProductCustomManager designerProjectItemsProductCustomManager;

    @Transactional
    public void delete(PickGoodsProduct record, String updateUser) {
        record.setDeleted(PickGoodsProduct.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria()
                .andPickProductIdEqualTo(record.getPickProductId())
                .andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
        BizAssert.isTrue(row == 1, "删除产品失败");

        // 刷新价格数据
        pickGoodsManager.refreshProductPrice(record.getPickGoodsId());

        // 验证上线数据
        pickGoodsManager.verifyProductOnline(record.getPickGoodsId());

        // 删除设计师项目数据
        designerProjectItemsProductManager.deleteByPickProductId(record.getPickProductId(), updateUser);
        designerProjectItemsProductCustomManager.deleteByPickProductId(record.getPickProductId(), updateUser);
    }


    public PickGoodsProduct getById(Integer pickProductId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(pickProductId, PickGoodsProduct.NOT_DELETED);
    }

    public List<PickGoodsProduct> queryByPickGoodsId(Integer pickGoodsId) {
        PickGoodsProductExample example = new PickGoodsProductExample();
        example.createCriteria().andPickGoodsIdEqualTo(pickGoodsId).andDeletedEqualTo(PickGoodsProduct.NOT_DELETED);
        example.orderBy(Column.specSorting.asc());
        return dao.selectByExample(example);
    }
}